{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 常见命令"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sort\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "# 根据年份排序\n",
    "# 2011-02-03\n",
    "sort -k 2.1,2.4 -t ' ' dates2.txt\n",
    "#数字排序\n",
    "#apple:10:2.5\n",
    "sort -k 2 -t : fruits.txt\n",
    "# 输出单词词频\n",
    " cat file_list/words.txt | tr -s ' ' '\\n' | sort | uniq -c | sort -r | awk '{print $2, $1}'\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## read\n",
    "```bash\n",
    "echo \"who are you\"\n",
    "read name\n",
    "echo \"I'am $name\"\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "a=5\n",
    "b=4\n",
    "if [ $a -ge $b ]\n",
    "#=\n",
    "#if test $a -ge $b\n",
    "then \n",
    "echo \"$a greater and equal than $b\"\n",
    "else\n",
    "echo \"$a less than $b\" \n",
    "fi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 常见语法结构"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## case"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "a=5\n",
    "b=4\n",
    "case \"$a\" in\n",
    "6) echo 1;;\n",
    "7) echo 2;;\n",
    "esac\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## while"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "a=5\n",
    "while [ $a -ge 0 ]\n",
    "do \n",
    "echo \"$a\"\n",
    "let a=$a-1\n",
    "done"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "## 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "func(){\n",
    "    echo \"Let's begin now\"\n",
    "    echo \"$a $b $c\"\n",
    "    echo \"$1 $2 $3\"\n",
    "}\n",
    "\n",
    "a='welcome'\n",
    "b=\"you\"\n",
    "c=\"!!!\"\n",
    "func huang xiao ming"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "source": [
    "## for"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "# 文件行业字符转置\n",
    "columns=$(cat file.txt | head -n 1 | wc -w)\n",
    "for i in $(seq 1 $columns)\n",
    "do\n",
    "awk '{print $'\"$i\"'}' file.txt | xargs\n",
    "done"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.4 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "3d597f4c481aa0f25dceb95d2a0067e73c0966dcbd003d741d821a7208527ecf"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
